package com.ruoyi.factorydata.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.factorydata.domain.vo.MaterialDataVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.factorydata.domain.MaterialData;
import com.ruoyi.factorydata.service.IMaterialDataService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 物料数据Controller
 * 
 * @author ruoyi
 * @date 2025-07-08
 */
@RestController
@RequestMapping("/materialdata/materialdata")
public class MaterialDataController extends BaseController
{
    @Autowired
    private IMaterialDataService materialDataService;

    /**
     * 查询物料数据列表
     */
    @PreAuthorize("@ss.hasPermi('materialdata:materialdata:list')")
    @GetMapping("/list")
    public TableDataInfo list(MaterialData materialData)
    {
        startPage();
        if(materialData != null && materialData.getCategoryId() != null){
            materialData.setCategoryId(materialData.getCategoryId() == 0 ? null : materialData.getCategoryId());
        }
        List<MaterialDataVo> list = materialDataService.selectMaterialDataList(materialData);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setRows(list);
        tableDataInfo.setTotal(new PageInfo(list).getTotal());
        return tableDataInfo;
    }

    /**
     * 导出物料数据列表
     */
    @PreAuthorize("@ss.hasPermi('materialdata:materialdata:export')")
    @Log(title = "物料数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MaterialData materialData)
    {
        List<MaterialDataVo> list = materialDataService.selectMaterialDataList(materialData);
        ExcelUtil<MaterialDataVo> util = new ExcelUtil<MaterialDataVo>(MaterialDataVo.class);
        util.exportExcel(response, list, "物料数据数据");
    }

    /**
     * 获取物料数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('materialdata:materialdata:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(materialDataService.selectMaterialDataById(id));
    }

    /**
     * 新增物料数据
     */
    @PreAuthorize("@ss.hasPermi('materialdata:materialdata:add')")
    @Log(title = "物料数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MaterialData materialData)
    {
        boolean exists = materialDataService.lambdaQuery().eq(MaterialData::getMaterialCode, materialData.getMaterialCode()).count() > 0;
        if (exists) {
            return AjaxResult.warn("物料编码已存在");
        }
        materialData.setCreateBy(getUserId().toString());
        return toAjax(materialDataService.insertMaterialData(materialData));
    }

    /**
     * 修改物料数据
     */
    @PreAuthorize("@ss.hasPermi('materialdata:materialdata:edit')")
    @Log(title = "物料数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MaterialData materialData)
    {
        boolean exists = materialDataService.lambdaQuery()
                .eq(MaterialData::getMaterialCode, materialData.getMaterialCode())
                .ne(MaterialData::getId, materialData.getId()).count() > 0;
        if (exists) {
            return AjaxResult.warn("物料编码已存在");
        }
        return toAjax(materialDataService.updateMaterialData(materialData));
    }

    /**
     * 删除物料数据
     */
    @PreAuthorize("@ss.hasPermi('materialdata:materialdata:remove')")
    @Log(title = "物料数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(materialDataService.deleteMaterialDataByIds(ids));
    }
}
